﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Diagnostics;
using System.Text.RegularExpressions;
using InHD.Properties;
using InHD.dataaccess;

namespace InHD.model
{
    class Category : IDataErrorInfo
    {
        #region Creation

        public static Category CreateCategory()
        {
            return new Category();
        }

        public static Category CreateCategoryInfo(
            string description,
            string name,
            string unk)
        {
            return new Category
            {
                Description = description,
                Name = name,
                CategoryUNK = unk
            };
        }

        protected Category()
        {
        }

        #endregion // Creation

        #region State Properties

        public string Description { get; set; }

        public string Name { get; set; }
        public string CategoryUNK { get; set; }

        #endregion // State Properties

        #region IDataErrorInfo Members

        string IDataErrorInfo.Error { get { return null; } }

        string IDataErrorInfo.this[string propertyName]
        {
            get { return this.GetValidationError(propertyName); }
        }

        #endregion // IDataErrorInfo Members

        #region Validation

        /// <summary>
        /// Returns true if this object has no validation errors.
        /// </summary>
        public bool IsValid
        {
            get
            {
                foreach (string property in ValidatedProperties)
                    if (GetValidationError(property) != null)
                        return false;

                return true;
            }
        }

        static readonly string[] ValidatedProperties = 
        { 
            "Name"
        };

        string GetValidationError(string propertyName)
        {
            if (Array.IndexOf(ValidatedProperties, propertyName) < 0)
                return null;

            string error = null;

            switch (propertyName)
            {
                case "Name":
                    error = this.ValidateName();
                    break;

                default:
                    Debug.Fail("Unexpected property being validated on CompanyInfo: " + propertyName);
                    break;
            }

            return error;
        }

        string ValidateName()
        {
            if (utilities.ValidatingUtilities.ValidateMissing(this.Name))
            {
                return Resources.InHD_Error_MissingField;
            }
            return null;
        }

        #endregion // Validation

        public Category CreateShallowCopy()
        {
            return (Category)this.MemberwiseClone();
        }
    }
}
